// 所有模块都通过 define 来定义
define(function(require, exports, module) {

	// 通过 require 引入依赖
	require('jquery');
	require('validator');
	require('layer');
	require('kindeditor');
	
	
	//删除操作
	$('.mini_del').click(function(){
    	
		var $this = $(this);
		var asker = layer.confirm('确定删除吗？', function(){
			var ii = layer.msg('数据处理中...', {icon: 16,time: 10000}, function(){
	    	    layer.alert('服务器响应超时', {icon: 7});
	    	});
			$.get($this.attr('data-url'),function(data){
				layer.close(ii);
				//data = JSON.parse(data);  //str 转 json
				if(data.status){
					layer.msg('删除成功', {icon: 1,time:1000},function(){
					    location.reload(); //自动关闭后可做一些刷新页面等操作
					});
				}else{
					layer.msg(data.info, {icon: 2,time:1000});
				}
			},'json');
		});
	});
	
	
	
	//表单验证
	$('.validator_form').validator({
   	    valid: function(form){
   	    	var me = this;
   	    	//防止重复提交表单
   	        me.holdSubmit();
   	        var ii = layer.msg('数据处理中...', {icon: 16,time: 10000}, function(){
	    	    layer.alert('服务器响应超时', {icon: 7});
	    	    me.holdSubmit(false);
	    	});
   	     	$.ajax({
	            url:  $(form).attr('action'),
	            data: $(form).serialize(),
	            type: "POST",
	            dataType:"json",
	            success: function(data){
	            	layer.close(ii);
	            	//data = JSON.parse(data);  //str 转 json
	            	if (data.status==1) {
	            		layer.msg(data.info, {icon: 1,time:1000});
	            		//关闭父窗口
	            		if($(form).hasClass('close-dialog')){
        					setTimeout(function(){
        						parent.close_iframe_dialog();
                        	},1000);
        				}
	                }else{
	                	layer.msg(data.info, {icon: 2,time:1000});
	                }
	            	//需要跳转的话
	            	if(data.url){
                    	setTimeout(function(){
                    		location.href= data.url;
                    	},1000);
                    }
	            	
	            	//信息提示框结束之后，表单可提交
	            	setTimeout(function(){
	            		me.holdSubmit(false);
                    },1000);
	            	
	            }
	        });
   	    }
   	});
	

	
    $('.ajax-post').click(function(){
    	var $this = $(this);
    	var target_form = $(this).attr('target-form');
    	var form = $('.'+target_form);
    	var query = form.serialize();
    	var msg = $this.attr('data-msg');
    	if(!query){
    		layer.alert('没有可操作的数据', {icon: 7});
    		return false;
    	}
    	
    	if(!msg){
    		msg = '确定删除吗？';
    	}
    	
    	
    	if($this.hasClass('confirm')){
    		var asker = layer.confirm(msg, function(){

    			var ii = layer.msg('数据处理中...', {icon: 16,time: 10000}, function(){
    	    	    layer.alert('服务器响应超时', {icon: 7});
    	    	});
    			
    			$.post($this.attr('data-url'),query,function(data){
    				layer.close(ii);
    				if(data.status){
    					layer.msg(data.info, {icon: 1,time:1000});
    					//关闭父窗口
    					if($this.hasClass('close-dialog')){
        					setTimeout(function(){
        						parent.close_iframe_dialog();
                        	},1000);
        				}
    					if($this.hasClass('shuaxin')){
    	            		setTimeout(function(){
    	            			location=location;
                        	},1000);
    	            	}
    				}else{
    					layer.msg(data.info, {icon: 2,time:1000});
    				}
    				
    				//需要跳转的话
    				if(data.info != 'You do not have this authority'){
    					if(data.url){
                        	setTimeout(function(){
                        		location.href= data.url;
                        	},1000);
                        }
    				}
	            	
    			},'json');
    		});
    	
    	}else{
    		var ii = layer.msg('数据处理中...', {icon: 16,time: 10000}, function(){
	    	    layer.alert('服务器响应超时', {icon: 7});
	    	});
    		
    		$.post($this.attr('data-url'),query,function(data){
    			layer.close(ii);
				if(data.status){
					layer.msg(data.info, {icon: 1,time:1000});
					//关闭父窗口
					if($this.hasClass('close-dialog')){
    					setTimeout(function(){
    						parent.close_iframe_dialog();
                    	},1000);
    				}
					
					if($this.hasClass('shuaxin')){
	            		setTimeout(function(){
	            			location=location;
                    	},1000);
	            	}
	            	
				}else{
					layer.msg(data.info, {icon: 2,time:1000});
				}
				
				//需要跳转的话
				if(data.info != 'You do not have this authority'){
					if(data.url){
                    	setTimeout(function(){
                    		location.href= data.url;
                    	},1000);
                    }
				}
			},'json');
		
    	}
    	return false;

    });

    $('.ajax-get').click(function(){
    	var $this = $(this);
    	var msg = $(this).attr('data-msg');
    	if(!msg){
    		msg = '确定删除吗？';
    	}
    	if($this.hasClass('confirm')){
    		var asker = layer.confirm(msg, function(){
    			var ii = layer.msg('数据处理中...', {icon: 16,time: 10000}, function(){
    	    	    layer.alert('服务器响应超时', {icon: 7});
    	    	});
    			$.get($this.attr('data-url'),function(data){
    				layer.close(ii);
    				if(data.status){
    					layer.msg(data.info, {icon: 1,time:1000});
    					//关闭父窗口
        				if($this.hasClass('close-dialog')){
        					setTimeout(function(){
        						parent.close_iframe_dialog();
                        	},1000);
        				}
        				//刷新父窗口
        				if($this.hasClass('parent-refresh')){
        					setTimeout(function(){
        						parent.refresh();
                        	},1000);
        				}
    					if($this.hasClass('shuaxin')){
    	            		setTimeout(function(){
    	            			location=location;
                        	},1000);
    	            	}
    				}else{
    					layer.msg(data.info, {icon: 2,time:1000});
    				}
    				
    				//需要跳转的话
    				if(data.info != 'You do not have this authority'){
    					if(data.url){
                        	setTimeout(function(){
                        		location.href= data.url;
                        	},1000);
                        }
    				}
	            	
    			},'json');
    		});
    	}else{
    		var ii = layer.msg('数据处理中...', {icon: 16,time: 10000}, function(){
	    	    layer.alert('服务器响应超时', {icon: 7});
	    	});
    		$.get($this.attr('data-url'),function(data){
    			layer.close(ii);
				if(data.status){
					layer.msg(data.info, {icon: 1,time:1000});
					//关闭父窗口
					if($this.hasClass('close-dialog')){
    					setTimeout(function(){
    						parent.close_iframe_dialog();
                    	},1000);
    				}
					if($this.hasClass('shuaxin')){
	            		setTimeout(function(){
	            			location=location;
                    	},1000);
	            	}
				}else{
					layer.msg(data.info, {icon: 2,time:1000});
				}
				//需要跳转的话
				if(data.info != 'You do not have this authority'){
					if(data.url){
                    	setTimeout(function(){
                    		location.href= data.url;
                    	},1000);
                    }
				}
			},'json');
    	}
    });

    /**
     * 刷新当前页面
     */
    window.refresh = function(){
    	location.replace(location.href);
    }
    
    var open_dialog;
    window.open_iframe_dialog = function(url_p,title_p,options_p,is_reload,ismaxmin){
    	if(!options_p){
    		options_p = ['1024px','500px'];
    	}
    	var iscbtn = 1;
    	if(!ismaxmin){
    		ismaxmin = false;
    		iscbtn = 0;
    	}else{
    		ismaxmin = true;
    	}

    	open_dialog = layer.open({
    	    type: 2,
    	    title: title_p,
    	    maxmin: ismaxmin,
    	    shadeClose: true,
    	    closeBtn: iscbtn,
    	    area: options_p,
    	    content: url_p, //iframe的url，no代表不显示滚动条
    	    end: function(){ //此处用于演示
    	    	if(is_reload){
            		location.reload();  //当前页面是否需要刷新
            	}
    	    }
    	});
    }
    window.close_iframe_dialog = function(){
    	layer.close(open_dialog);
    }

    
});
